<form action="{:sysuri()}" method="post" class="layui-form layui-card" id="claim_form">

    <div class="layui-card">
        <div class="layui-card-header">{:lang('推送设置')}</div>
        <div class="layui-card-body">
            <div class="layui-form-item">
                <div class="layui-inline">
                  <label class="layui-form-label label-required-next">{:lang('默认库存')}</label>
                  <div class="layui-input-inline">
                        <input type="number" name="stock" lay-verify="required" lay-affix="number" placeholder="{:lang('请输入默认库存')}" min="1" max="1000000" class="layui-input">
                  </div>
                </div>
            </div>
        </div>
    </div>

    <div class="layui-card">
        <div class="layui-card-header">{:lang('目标店铺')}</div>
        <div class="layui-card-body">
            {if !empty($shop_data)}
            {foreach $shop_data as $item}
            <div class="layui-form-item">
                <label class="layui-form-label">{$item.name|default=''}</label>
                <input type="hidden" name="ids" value="{$good_ids|default=''}">
                <div>
                    <input type="checkbox" name="close" lay-skin="switch" lay-filter="class_all" lay-text="{:lang('全选')}|{:lang('全不选')}">&nbsp;&nbsp;
                    <input type="checkbox" name="close" lay-skin="switch" lay-filter="class_fan" lay-text="{:lang('反选')}|{:lang('取消反选')}">
                </div>
                <div class="layui-input-block">
                    {if !empty($item['sub_data']) }
                    {foreach $item['sub_data'] as $sub_item}
                    <input type="checkbox" name="shop[{$sub_item.platform_id|default=0}][]" value="{$sub_item.id|default=''}" title="{$sub_item.name|default=''}">
                    {/foreach}
                    {/if}
                </div>
            </div>
            {/foreach}
            {/if}
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block" style="margin-left: 20px;">
            说明:<br>
            1、产品推送到目标平台的草稿箱中，需要在各个平台完善产品信息才能推送到各个平台；
        </div>
    </div>

    <div class="layui-form-item text-center">
        <button class="layui-btn" type='button' lay-submit lay-filter="claim_btn">{:lang('认领')}</button>
        <button class="layui-btn layui-btn-danger" type='button' data-confirm="{:lang('确定要取消认领吗？')}" data-close>{:lang('取消认领')}</button>
    </div>
</form>
<script>
    layui.use(['form'], function(){
        let form = layui.form;
        // 提交
        form.on('submit(claim_btn)', function(data){
            let field = data.field;
            if(!isNotEmpty(field.stock)){
                $.msg.error('{:lang("请输入默认库存")}')
                return false;
            }
            let result = {}
            $.each(field, function (key, val) {
                // 解析键
                let match = key.match(/shop\[(\d+)\]\[(\d+)\]/);
                if (match) {
                    let outerIndex = parseInt(match[1], 10);
                    let innerIndex = parseInt(match[2], 10);

                    // 构建数组
                    if (!result[outerIndex]) {
                        result[outerIndex] = [];
                    }

                    result[outerIndex].push(val);
                    delete field[key];
                }
            })
            if(!isNotEmpty(result)){
                $.msg.error('{:lang("请选择目标店铺")}')
                return false;
            }
            field.shop = result;
            $.ajax({
                url: "{:sysuri()}",
                type: "post",
                data: {data: JSON.stringify(field)},
                dataType: "json",
                success: function (res) {
                    if (res.code === 1) {
                        $.msg.success(res.info, 3, function () {
                            $.msg.closeThisModal('#claim_form')
                        });
                        return false;
                    }else{
                        $.msg.error(res.info);
                        return false;
                    }
                }
            });
        })

        // 全选/全不选
        form.on('switch(class_all)', function(data){
            $(this).parent().siblings().find('input').prop('checked', data.elem.checked);
            form.render('checkbox');
        });

        // 反选
        form.on('switch(class_fan)', function(data){
            $(this).parent().siblings().find('input').each(function(){
                this.checked = !this.checked;
            });
            form.render('checkbox');
        });
    })
</script>